package com.activemanager.healthGadget;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.activemanager.dashboard.H0Activity;
import com.activemanager.myHealthProfile.Q1MainActivity;
import com.example.android.navigationdrawerexample.R;
import com.example.android.navigationdrawerexample.R.layout;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class T3Fragment  extends Fragment {
	public static final String ARG_PAGE = "page";
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final View rootView = inflater.inflate(R.layout.t3, container, false);
		initPic(rootView);
		LinearLayout typeLayout = (LinearLayout) rootView.findViewById(R.id.typeLayout);
		LinearLayout activityLayout = (LinearLayout) rootView.findViewById(R.id.activityLayout);
		activityLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final Dialog dialog = new Dialog(getActivity(),
						R.style.MyDialog);
				final BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;

				dialog.setContentView(R.layout.p30);
				final LinearLayout img = (LinearLayout) dialog
						.findViewById(R.id.linearLayout);
//				InputStream is = getResources().openRawResource(R.drawable.u165_normal);
//				Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//				img.setBackgroundDrawable(new BitmapDrawable(bit));
				img.setBackgroundResource(R.drawable.bg_for_popup_54);
				
				final TextView textview = (TextView) rootView
						.findViewById(R.id.textView5);
				
				
				final TextView tv = (TextView) dialog
						.findViewById(R.id.textView1);
				tv.setText("ACTIVITY");
				dialog.show();
				Window dialogWindow = dialog.getWindow();
				WindowManager m = getActivity().getWindowManager();
				Display d = m.getDefaultDisplay();
				WindowManager.LayoutParams p = dialogWindow.getAttributes();
				p.height = (int) (d.getHeight() * 0.8);
				p.width = (int) (d.getWidth() * 0.9);
				dialogWindow.setAttributes(p);

				String[] mFrom = new String[] { "profile_img", "profile_item" };
				String[] profileTitle = { "Walking", "Jogging", "Cycling",
						"Badminton" };
				String[] resName = new String[]{"choose_u159_normal","choose_u155_normal","choose_u158_normal","choose_u156_normal"};
				int[] mTo = new int[] { R.id.profile_img, R.id.profile_item };
				List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
				Map<String, Object> mMap = null;
				for (int i = 0; i < profileTitle.length; i++) {
					mMap = new HashMap<String, Object>();
					int drawable = getResources().getIdentifier(
							resName[i], "drawable",
							getActivity().getPackageName());
					mMap.put("profile_img", drawable);
					mMap.put("profile_item", profileTitle[i]);
					mList.add(mMap);
				}
				final ListView mListView = (ListView) dialog
						.findViewById(R.id.list_view);
				SimpleAdapter mAdapter = new SimpleAdapter(getActivity(),
						mList, R.layout.drawer_list_item, mFrom, mTo);
				mListView.setAdapter(mAdapter);
				mListView.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int pos, long arg3) {
						// TODO Auto-generated method stub
						if(pos!=0 && pos != 4){
						HashMap<String,String> map = (HashMap<String, String>) mListView.getItemAtPosition(pos);
						String text = map.get("profile_item");
						textview.setText(text);
						dialog.dismiss();
						}
					}
					
				});
				
				
				Button btn_cancel = (Button) dialog
						.findViewById(R.id.button1);
				btn_cancel.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				
			}
		});
		
		typeLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final Dialog dialog = new Dialog(getActivity(),
						R.style.MyDialog);
				final BitmapFactory.Options opt = new BitmapFactory.Options();
				opt.inPreferredConfig = Bitmap.Config.RGB_565;
				opt.inPurgeable = true;
				opt.inInputShareable = true;

				dialog.setContentView(R.layout.p7);
				final LinearLayout img = (LinearLayout) dialog
						.findViewById(R.id.linearLayout);
//				InputStream is = getResources().openRawResource(R.drawable.u165_normal);
//				Bitmap bit = BitmapFactory.decodeStream(is, null, opt);
//				img.setBackgroundDrawable(new BitmapDrawable(bit));
				img.setBackgroundResource(R.drawable.bg_for_popup_54);
				
				final TextView textview = (TextView) rootView
						.findViewById(R.id.textView6);
				
				
				final TextView tv = (TextView) dialog
						.findViewById(R.id.textView1);
				tv.setText("DATA TYPE");
				dialog.show();
				Window dialogWindow = dialog.getWindow();
				WindowManager m = getActivity().getWindowManager();
				Display d = m.getDefaultDisplay();
				WindowManager.LayoutParams p = dialogWindow.getAttributes();
				p.height = (int) (d.getHeight() * 0.8);
				p.width = (int) (d.getWidth() * 0.9);
				dialogWindow.setAttributes(p);

				String[] mFrom = new String[] {"choose_item"};
				int[]  mTo =new int[] {  R.id.profile_item};
				String[] chooseTitle = { "Mean plantar pressure", "Peak pantar pressure", "Pressure - Time Intetral", "Force - Time Intergral", "Force distribution" };
				List mList = new ArrayList<String>();
				for (int i = 0; i < chooseTitle.length; i++) {
					Map<String, Object> mMap = new HashMap<String,Object>();
					mMap.put("choose_item", chooseTitle[i]);
					mList.add(mMap);
				}
				final ListView mListView = (ListView) dialog.findViewById(R.id.list_view);
				SimpleAdapter mAdapter = new SimpleAdapter(getActivity(), mList,	R.layout.pure_text_list_item, mFrom, mTo);
				mListView.setAdapter(mAdapter);
				mListView.setOnItemClickListener(new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int pos, long arg3) {
						// TODO Auto-generated method stub
						if(pos!=0 && pos != 4){
						HashMap<String,String> map = (HashMap<String, String>) mListView.getItemAtPosition(pos);
						String text = map.get("choose_item");
						textview.setText(text);
						dialog.dismiss();
						}
					}
					
				});
				
				
				Button btn_cancel = (Button) dialog
						.findViewById(R.id.button1);
				btn_cancel.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});
				
			}
		});
		
		return rootView;
	}
	
	private void initPic(View rootView) {
		// TODO Auto-generated method stub
		final BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inPreferredConfig = Bitmap.Config.RGB_565;
		opt.inPurgeable = true;
		opt.inInputShareable = true;
		
		final Animation fadeInAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.fade);
		
		
		ImageView img2 = (ImageView) rootView.findViewById(R.id.imageView1);
		final ImageView img3 = (ImageView) rootView.findViewById(R.id.imageView2);
		InputStream is2 = getActivity().getResources().openRawResource(
				R.drawable.foot_update_11);
		Bitmap bit2 = BitmapFactory.decodeStream(is2, null, opt);
		
		img2.setBackgroundDrawable(new BitmapDrawable(bit2));
		img2.setAnimation(fadeInAnimation);
new Handler().postDelayed(new Runnable(){
			
			@Override
			public void run() {
				InputStream is3 = getActivity().getResources().openRawResource(
						R.drawable.foot_update_09);
				Bitmap bit3 = BitmapFactory.decodeStream(is3, null, opt);
				img3.setBackgroundDrawable(new BitmapDrawable(bit3));
			}
			}, 2000);

		
		
		
	}

	public static T3Fragment newInstance(String text) {

		 T3Fragment f = new T3Fragment();
	        Bundle b = new Bundle();
	        b.putString("msg", text);

	        f.setArguments(b);

	        return f;
	    }
}
